package com.totcy.atom.view.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.animation.DecelerateInterpolator;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.totcy.atom.R;
import com.totcy.atom.adapter.XImageGridAdapter;
import com.totcy.atom.bean.ImagesBean;
import com.totcy.atom.presenter.ImageGridPresenter;
import com.totcy.atom.presenter.contract.ImageGridContract;
import com.totcy.baselibrary.adapter.BaseLoadMoreAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * description: xxxiao 图片网格
 * author: Totcy
 * date: 2017/11/4 22:01
 * update: 2017/11/4
 */
public class ImageGridActivity extends BaseFullActivity<ImageGridContract.View,ImageGridPresenter> implements ImageGridContract.View, OnRefreshListener, BaseLoadMoreAdapter.OnItemClickListener {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;

    private List<ImagesBean> list = new ArrayList<>();
    private List<String> imageUrls = new ArrayList<>();
    private XImageGridAdapter adapter;

    /**
     * @return 获取标题名
     */
    @Override
    public String getTitleName() {
        return getIntent().getStringExtra("title");
    }

    /**
     * 布局Id 解析添加到 layout/layout_full_base_activity.xml中
     *
     * @return 子activity layout id
     */
    @Override
    public int getLayoutId() {
        return R.layout.activity_image_list;
    }

    /**
     * 初始化View
     */
    @Override
    public void initView() {

        GridLayoutManager lm = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(lm);
        adapter = new XImageGridAdapter(this, list);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        //设置回弹动画插值器 减速效果
        refresh.setReboundInterpolator(new DecelerateInterpolator());
        refresh.setOnRefreshListener(this);
        //自动刷新
        refresh.autoRefresh(200);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        getPresenter().getImagesData(getIntent().getStringExtra("linkUrl"));
    }

    @Override
    public void onItemClick(int position) {
        ArrayList<String> urlList = new ArrayList<>();
        urlList.addAll(imageUrls);
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        bundle.putStringArrayList("urlList", urlList);
        openActivity(PictureBrowsingActivity.class, bundle);
    }

    /**
     * 妹子图片图集 数据
     *
     * @param images
     */
    @Override
    public void onImagesData(List<ImagesBean> images) {
        refresh.finishRefresh(true);

        list.clear();
        list.addAll(images);
        adapter.notifyDataSetChanged();

        setUrls();
    }

    /**
     * 获取图片list
     */
    private void setUrls() {

        imageUrls.clear();

        Observable.fromIterable(list).subscribe(new Consumer<ImagesBean>() {
            @Override
            public void accept(@NonNull ImagesBean imagesBean) throws Exception {
                imageUrls.add(imagesBean.getUrl());
            }
        });
    }

    /**
     * 这里获取数据失败
     */
    @Override
    public void onDataFail() {
        refresh.finishRefresh(false);

    }


}
